/**
 * dom的相关操作
 */

export const getStyle = (selector: any) => {
  const currentDom = document.querySelector(selector)
  if (currentDom) {
    if (currentDom.currentStyle) {
      return currentDom.currentStyle
    }
    return (document.defaultView as Window)?.getComputedStyle(currentDom as Element, null)
  }
  return null
}

/**
 * Check if an element has a class
 * @param {HTMLElement} elm
 * @param {string} cls
 * @returns {boolean}
 */
export function hasClass(ele: HTMLElement, cls: string) {
  return !!ele?.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
}

/**
 * Add class to element
 * @param {HTMLElement} elm
 * @param {string} cls
 */
export function addClass(ele: HTMLElement, cls: string) {
  if (!hasClass(ele, cls)) {
    ele && (ele.className += ' ' + cls)
  }
}

/**
 * Remove class from element
 * @param {HTMLElement} elm
 * @param {string} cls
 */
export function removeClass(ele: HTMLElement, cls: string) {
  if (hasClass(ele, cls)) {
    const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
    ele.className = ele.className.replace(reg, ' ')
  }
}
